package com.goldcn.user.service;

import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.enums.CfgExtEnum;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.user.dao.ConfigMapper;
import com.goldcn.user.dto.ClazzPriceRate;
import com.goldcn.user.model.Config;
import com.goldcn.util.CommonTools;
import com.goldcn.util.JsonUtil;
import com.google.common.base.Objects;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Created by 1111 on 2017/3/17.
 */
@Service
public class ConfigService {
    private Logger logger = Logger.getLogger(ConfigService.class);

    @Autowired
    ConfigMapper configMapper;

    @Autowired
    RestTemplate restTemplate;

    //1增 2改
    public Object addUpdateConfig(Config config,int type){
        switch (type){
            case 1:
                config.setCompanyInfoID(IdGen.get().nextId());
                configMapper.insertSelective(config);
                break;
            case 2:
                configMapper.updateByPrimaryKeySelective(config);
                break;
        }
        return "success";
    }

    //删
    public Object deleteConfig(Long id){
        configMapper.deleteByPrimaryKey(id);
        return "success";
    }

    //按id查
    public Object getConfigByID(Long id){
        return configMapper.selectByPrimaryKey(id);
    }

    //查所有
    public Object getConfigAll(){
        return configMapper.selectAll();
    }


    public boolean  isOpenErp() {
        Config config = configMapper.selectByPrimaryKey(SystemContext.getCompanyID());
        if (config!=null) {
            return "1".equalsIgnoreCase(config.getIsOpenErp());
        } else {
            logger.error("isOpenErp 调用base服务失败");
        }
        return false;
    }

    public boolean  isOpenGoldPoint() {
        Config config = configMapper.selectByPrimaryKey(SystemContext.getCompanyID());
        if (config!=null) {
            return "1".equalsIgnoreCase(config.getIsOpenGoldPoint());
        } else {
            logger.error("isOpenGoldPoint 调用base服务失败");
        }
        return false;
    }

    public boolean isClazzSupply(Long companyID){
        return clazzPriceRate(companyID,"isOpenBonus");
    }

    private boolean clazzPriceRate(Long companyID,String type){
        try {
            Config config= (Config) getConfigByID(companyID);
            if(config == null || CommonUtils.isEmpty(config.getClazzPriceRate())){
                return false;
            }
            String clazzPriceRate = config.getClazzPriceRate();
            if(!clazzPriceRate.startsWith("{")){
                return false;
            }
            ClazzPriceRate clazzPriceRateModel = (ClazzPriceRate) JsonUtil.toBean(clazzPriceRate,ClazzPriceRate.class);
            if(Objects.equal(type,"isOpenBonus")){
                return Objects.equal(clazzPriceRateModel.getIsOpenBonus(),1) ? true : false;
            }else if(Objects.equal(type,"employeeChange")){
                return Objects.equal(clazzPriceRateModel.getEmployeeChange(),1) ? true : false;
            }else if(Objects.equal(type,"isClazzSupply")){
                return Objects.equal(clazzPriceRateModel.getIsClazzSupply(),1) ? true : false;
            }
            return false;
        } catch (Exception e) {
           return false;
        }catch (Error e){
            return false;
        }
    }

    public Object queryDiamondClazzID() {
        try {
            return CommonTools.getExtString(restTemplate, CfgExtEnum.DIAMONDCLAZZID.getCode());
        } catch (Exception e) {
            return "";
        }
    }

}
